package com.ssy.lingxi.marketing.controller.callback;

import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.model.constant.MarketingConstants;
import com.ssy.lingxi.marketing.model.constant.TaskTypeEnum;
import com.ssy.lingxi.marketing.service.IMarketingScheduleCallBackService;
import com.ssy.lingxi.scheduler.api.model.ScheduleTaskCallbackVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 *  定时任务回调入口
 * @author ds
 * @date 2021/7/23
 * @version 2.0.0
 */
@Slf4j
@RestController
public class MarketingScheduleFeignController extends BaseController {

    @Resource
    private IMarketingScheduleCallBackService activityScheduleCallBackService;

    /**
     * 定时任务回调接口
     * */
    @PostMapping(value = MarketingConstants.TASK_CALLBACK_URL)
    public Wrapper<Void> taskCallback(@RequestBody @Valid ScheduleTaskCallbackVO callbackVO) {
        log.info("MarketingFeignController:taskCallback:callbackVO:{}", JSONUtil.toJsonStr(callbackVO));
        Map<String, Object> taskAttributes = callbackVO.getTaskAttributes();
        if (taskAttributes != null) {
            Integer taskType = (Integer) taskAttributes.get(TaskTypeEnum.Constants.NAME);
            if (TaskTypeEnum.PF_ACTIVITY_SIGN_UP_START.getCode().equals(taskType)) {
                // 启动平台活动报名任务
                activityScheduleCallBackService.startPfActivitySignUpTask(callbackVO);
            } else if (TaskTypeEnum.PF_ACTIVITY_SIGN_UP_END.getCode().equals(taskType)) {
                // 结束平台活动报名任务
                activityScheduleCallBackService.endPfActivitySignUpTask(callbackVO);
            } else if (TaskTypeEnum.PF_ACTIVITY_END.getCode().equals(taskType)) {
                //  结束平台活动任务
                activityScheduleCallBackService.endPfActivityTask(callbackVO);
            } else if (TaskTypeEnum.MC_ACTIVITY_END.getCode().equals(taskType)) {
                // 结束商家活动任务
                activityScheduleCallBackService.endMcActivityTask(callbackVO);
            } else if (TaskTypeEnum.PF_COUPON_RELEASE_START.getCode().equals(taskType)) {
                // 开始平台优惠券发放任务
                activityScheduleCallBackService.startPfCouponReleaseTask(callbackVO);
            } else if (TaskTypeEnum.PF_COUPON_RELEASE_END.getCode().equals(taskType)) {
                // 结束平台优惠券发放任务
                activityScheduleCallBackService.endPfCouponReleaseTask(callbackVO);
            } else if (TaskTypeEnum.MC_COUPON_RELEASE_START.getCode().equals(taskType)) {
                // 开始商家优惠券发放任务
                activityScheduleCallBackService.startMcCouponReleaseTask(callbackVO);
            } else if (TaskTypeEnum.MC_COUPON_RELEASE_END.getCode().equals(taskType)) {
                // 结束商家优惠券发放任务
                activityScheduleCallBackService.endMcCouponReleaseTask(callbackVO);
            }
        }
        return Wrapper.success();
    }
}
